package com.amt.olcacon.utils;

import io.minio.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@Slf4j
@Component
public class MinioUtil {

    private static final String CONTENTTYPE_DOC = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

    private final MinioProperties minioProperties;
    private final MinioClient minioClient;

    public MinioUtil(MinioProperties minioProperties) {
        this.minioProperties = minioProperties;

        this.minioClient = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();

        log.info("MinioUtil initialized, endpoint={}", minioProperties.getEndpoint());
    }

    public MinioClient getClient() {
        return minioClient;
    }

    public String getBucket() {
        return minioProperties.getBucketName();
    }

    public String getSinkPath() {
        return minioProperties.getSinkPath();
    }
    public String upload(String bucketName, String objectName, InputStream stream, long size) throws Exception {
        // 确保 bucket 存在
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }

        // 上传文件
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(stream, size, -1)
                        .contentType(CONTENTTYPE_DOC)
                        .build()
        );

        // 返回可访问的 URL（前提：MinIO 配置了反向代理或开放端口）
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .method(Method.GET)
                        .build()
        );
    }

    /**
     * 从minio获取文件流
     */
    public InputStream getFileStream(String filePath) {
        try {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(minioProperties.getBucketName())
                            .object(filePath)
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String uploadAndGetDocUrl(ByteArrayOutputStream out, String bucketName, String docName) throws Exception {
        byte[] fileBytes = out.toByteArray();
        InputStream reportStream = new ByteArrayInputStream(fileBytes);
        return this.upload(
                bucketName,
                docName,
                reportStream,
                fileBytes.length
        );
    }

}